package cn.edu.scau.cmi.linzhanyi.composite;


import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.*;

public class Controller {
    @FXML
    private ComboBox<Component> combobox;
    @FXML
    private ComboBox<Component> combobox1;
    @FXML
    private Button say;
    @FXML
    private Button say1;
    @FXML
    private Button go;
    @FXML
    private Button back;
    @FXML
    private TextArea text;
    @FXML
    private Label group;
    private Component currentpeople;
    private Component groupmap;
    private Component currentgroup;

    @FXML
    private void initialize(){
        Group root = new Group("学校");
        Group group = new Group("数学与信息学院");
        Group group1 = new Group("艺术学院");
        Group group2 = new Group("6721");
        root.addAll(group,group1,new People("蔡徐坤"));
        group.addAll(new People("蔡徐坤鸡你太美"));
        group1.addAll(new People("蔡徐坤鸡你实在太美"),group2);
        currentgroup=root;
        go();





    }

    @FXML
    public void go(){

        ObservableList<Component> observableList = FXCollections.observableArrayList();
        ObservableList<Component> observableList1 = FXCollections.observableArrayList();
        if (currentgroup.get()!=null)
        for(Component c1:currentgroup.get()){
            if (c1 instanceof People){

                observableList.add(c1);

            }
            else{
                observableList1.add(c1);
            }
        }
        group.setText(currentgroup.toString());
        groupmap=currentgroup;
        text.setText(null);
        say.setDisable(true);
        say1.setDisable(true);
        go.setDisable(true);
        if(currentgroup.getParent()==null){
            back.setDisable(true);
        }
        else{
            back.setDisable(false);
        }
        if(currentgroup.get()==null){
            say1.setDisable(true);
        }
        else{
            say1.setDisable(false);
        }

        combobox.setItems(observableList);
        combobox.setValue(null);
        combobox1.setItems(observableList1);
        combobox1.setValue(null);
    }



    @FXML
    public void groupselect(){
        if (combobox1.getSelectionModel().getSelectedItem()!=null){
            currentgroup = (Component) combobox1.getSelectionModel().getSelectedItem();
            go.setDisable(false);

        }

    }
    @FXML
    public void peopleselect(){
        if (combobox.getSelectionModel().getSelectedItem()!=null){
            currentpeople = (Component) combobox.getSelectionModel().getSelectedItem();
            say.setDisable(false);
        }

    }
    @FXML
    public void say(){

        text.setText( currentpeople.say(""));
    }
    @FXML
    public void say1(){

        text.setText( groupmap.say(""));
    }
    @FXML
    public void back(){
        currentgroup = currentgroup.getParent();

        go();
    }


}
